﻿using Autofac;
using Microsoft.Extensions.DependencyInjection;

namespace Zero.WebAPI.Framework.Helper
{
    public class IOCHelper
    {
        private static IServiceProvider ServiceProvider;
        private static IContainer Container;

        public static void SetServiceProvider(IServiceProvider servicesProvider)
        {
            ServiceProvider = servicesProvider;
        }

        public static void SetContainer(IContainer container)
        {
            Container = container;
        }

        public static T GetService<T>() where T : class
        {
            if (ServiceProvider == null && Container == null)
                throw new Exception("容器未初始化");
           
            return ServiceProvider?.GetService<T>() ?? Container.Resolve<T>();
        }
    }
}
